(function () {
  let cartUl = document.querySelector("ul");
  AG("cart/list", { id: localStorage.getItem("uid") }, true).then((resData) => {
    console.log(resData.data);
    let cartDatas = resData.data.cart;
    let html = "";
    cartDatas.forEach((item) => {
      html += `
      <li>
        <input type="checkbox" class="ck" data-id=${item.goods_id}>
        <img src="${item.img_small_logo}">
        <span>${item.title}</span>
        <span>${item.current_price}</span>
        <span>-</span>
        <input type="number" value="${item.cart_number}">
        <span>+</span>
        <span>${item.current_price * item.cart_number}</span>
        <span data-id=${item.goods_id} class="del">删除</span>
        </li>
        `;
      cartUl.innerHTML = html;

      let aDels = document.querySelectorAll(".del");
      aDels.forEach((item) => {
        item.addEventListener("click", () => {
          AG(
            "/cart/remove",
            {
              id: localStorage.getItem("uid"),
              goodsId: item.dataset.id,
            },
            true
          ).then((resData) => {
            if (resData.data.code == 1) {
              location.reload();
            }
          });
        });
      });

      let aCks = document.querySelectorAll(".ck");
      aCks.forEach((item) => {
        item.addEventListener("click", () => {
          AP(
            "/cart/select",
            { id: localStorage.getItem("uid"), goodsId: item.dataset.id },
            true
          );
        });
      });

      let delSelBtn = document.querySelector("button");
      delSelBtn.addEventListener("click", () => {
        AG("/cart/remove/select", { id: localStorage.getItem("uid") }).then(
          (resData) => {
            console.log(resData.data);
            location.reload();
          }
        );
      });
    });
  });
})();
